var app = angular.module('app');

app.factory('bookService', ['$http', '$q', 'server',
    function ($http, $q, server) {
        var bookService = {};

        bookService.list = function (query, page) {
            var deferred = $q.defer();
            query = query || {};
            if (page || page === 0) {
                query.page = page;
            }
            $http({
                method: 'GET',
                url: server.addr + '/book',
                params: query
            }).then(function (response) {
                // store activity list result
                deferred.resolve(response.data);
            }, function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        };
        // get book by id
        bookService.getById = function (id) {
            var deferred = $q.defer();
            $http({
                method: 'GET',
                url: server.addr + '/book/' + id
            }).then(function (response) {
                // store activity list result
                deferred.resolve(response.data);
            }, function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        };
        // add video
        bookService.add = function (book) {
            var deferred = $q.defer();
            $http({
                method: 'POST',
                url: server.addr + '/book/',
                data: book
            }).then(function (response) {
                // store activity list result
                deferred.resolve(response.data);
            }, function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        };

        bookService.update = function (book) {
            var deferred = $q.defer();
            $http({
                method: 'PUT',
                url: server.addr + '/book/' + book._id,
                data: book
            }).then(function (response) {
                deferred.resolve(response.data);
            }, function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        };
        // delete video
        bookService.delete = function (book) {
            var deferred = $q.defer();
            $http({
                method: 'DELETE',
                url: server.addr + '/book/' + book._id,
            }).then(function (response) {
                deferred.resolve(response.data);
            }, function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        };

        bookService.searchByIsbn = function (isbn) {
            var deferred = $q.defer();
            $http({
                method: 'GET',
                url: server.addr + '/book/isbn/' + isbn,
            }).then(function (response) {
                // store activity list result
                deferred.resolve(response.data);
            }, function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        };
        return bookService;
    }]);